package com.cuckoo.mall.thus.order.adapter;

import android.support.annotation.Nullable;
import android.text.Html;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bgn.baseframe.utils.BaseStringUtil;
import com.bgn.baseframe.utils.imageloader.ImageLoader;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cuckoo.mall.R;
import com.cuckoo.mall.javabean.CartProductItem;
import com.cuckoo.mall.javabean.Product;
import com.cuckoo.mall.javabean.ProductAttribute;

import java.util.List;

/**
 * Created by Administrator on 2018/3/31.
 */

public class OrderProductAdapter extends BaseQuickAdapter<CartProductItem, BaseViewHolder> {

    private int size = 0;
    private boolean isFromProduct = false;

    public OrderProductAdapter(@Nullable List<CartProductItem> data) {
        super(R.layout.list_item_order_good, data);
        size = data.size();
    }

    public OrderProductAdapter(@Nullable List<CartProductItem> data, boolean value) {
        super(R.layout.list_item_order_good, data);
        size = data.size();
        isFromProduct = value;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final CartProductItem item) {
        //helper.setText(R.id.tv_property, "暂放");
        if (isFromProduct) {
            String atriibute = "";
            for (ProductAttribute propertyItem : item.getProduct_info().getAttribute_list()) {
                atriibute += propertyItem.getSelectedValue() + ",";
            }
            if (!TextUtils.isEmpty(atriibute)) {
                atriibute = atriibute.substring(0, atriibute.length() - 1);
            }
            helper.setText(R.id.tv_property, atriibute);
            helper.setText(R.id.tv_counts, "x" + item.getProduct_info().getNumber());
        } else {
            helper.setText(R.id.tv_property, BaseStringUtil.getAtributeSting(item.getAttribute()));
            helper.setText(R.id.tv_counts, "x" + item.getNumber());
        }


        if (item.getProduct_info() == null) {
            /*判空，避崩*/
            item.setProduct_info(new Product());
        }

        Product itemData = item.getProduct_info();
        helper.setText(R.id.tv_name, itemData.getName());

        helper.setText(R.id.tv_price, itemData.getCurrency() + " " + BaseStringUtil.getPrioceFormatString(itemData.getPrice()));
        if (itemData.getMarket_price() != 0) {
            //helper.setText(R.id.tv_max_price, Html.fromHtml("<strike>" + itemData.getCurrency() + " " + BaseStringUtil.getPrioceFormatString(itemData.getMarket_price()) + "</strike>"));
            helper.setText(R.id.tv_max_price, BaseStringUtil.addDeleteLineToText(itemData.getCurrency() + " " + BaseStringUtil.getPrioceFormatString(itemData.getMarket_price())));
        } else {
            helper.setText(R.id.tv_max_price, "");
        }

        ImageLoader.load(itemData.getImage()).into((ImageView) helper.getView(R.id.iv_image));
        if (helper.getAdapterPosition() == size - 1) {
            helper.setGone(R.id.view_line, false);
        } else {
            helper.setGone(R.id.view_line, true);
        }
    }
}
